'''
绘制网络的局部结构(如某个节点及其所有邻居所组成的ego网络)'''
from xml.dom.minicompat import NodeList
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
def plot_ego(graph,node):
    """绘制节点的局部网络（找一些度大小合适的节点尝试）
       使用nx的布局算法可视化结构,注意避免结构太大,复杂可能导致绘制失败,或者杂乱"""
    dg=dict(nx.degree(graph))#所有点的度
    d1=dg[node]
    nodelist=[]
    neigh_list=list(graph.neighbors(node))#指定节点的邻节点
    nodelist.append(node)
    print(d1)
    if d1>=50:#全部的度不超过50
        n=0
        for node_nei in neigh_list:
            if n>50:
                print("该点度超过50")
                break
            else:
                nodelist.append(node_nei)
                n=n+1
    else:
        for i in neigh_list:
            nodelist.append(i)
            if dg[i]+d1<50:
                d1=dg[i]+d1
                for node_ in list(graph.neighbors(i)):
                    if node_ not in nodelist:
                        nodelist.append(node_)
    print(nodelist)
    graph1=graph.subgraph(nodelist)
    color_map=[]
    for no in graph1.nodes():
        if no==node:
            color_map.append('red')
        else:
            color_map.append('blue')
    nx.draw(graph1,with_labels=True,node_color=color_map)
    plt.show()
def plotdgree_distribution(graph):
    '''绘制度的分布图'''
    d=dict(nx.degree(graph))
    x=list(range(max(d.values())+1))#有哪些度的值出现
    d_list=nx.degree_histogram(graph)#返回所有度的频数列表
    y=np.array(d_list)/graph.number_of_nodes()#计算频率
    plt.xlabel('degree')
    plt.ylabel('pro of degree')
    #plt.plot(x,y,color='red',linestyle='-',marker=',')
    plt.bar(x,y)
    plt.grid()
    plt.show()